/**
 * 流程列表
 * Created by douxc on 2015/4/21.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('SteelCtrl', ['$scope', 'MHService', function ($scope, MHService) {
      $scope.URL = MHService.URL.procedure;
    }]);
    app.register.directive('procedureList', function () {
      return {
        restrict: 'E',
        replace: true,
        scope: {
          queryUrl: '='
        },
        controller: function ($scope, $routeParams, MHService, $location) {
          var tUrl = $scope.queryUrl + '?gardenid=' + MHService.globalData(false, {key: 'projectId'}) + '&processid=' + $routeParams['flag'];
          MHService.globalData(true, {key: 'processId', value: $routeParams['flag'].split('?')[0]});//将当前的工序Id保存，备用
          MHService.pageQuery({url: tUrl}, function (data) {
            $scope.procedures = data['data'];//工序列表
            if (data['data'] && data['data'].length > 0) {
              angular.forEach(data['data'], function (obj) {
                //  alert(data.procedures.isfinish)
                obj.isCurrent = (data.procedures && (data.procedures.id === obj.id)&&(data.procedures.islastfinish!=1));//当前进行的工序
              });

            }
            $scope.isStart = data.isStart;
            if (data.gprid) {
              $scope.gprid = data.gprid;
            } else {
              //MHService.Toast('');
            }
            $scope.ischeckedUser = data.ischeckedUser;
            //  保存applyid,审批申请需要用
            MHService.globalData(true, {key: 'currentProcedures', value: data.procedures});
          });
          //开始流程
          $scope.startProcess = function (id) {
            MHService.postReq({
              url: MHService.URL.start_procedure + '?gprid=' + id,
            }, function (data) {
              console.log(data);
              $scope.isStart = !data.success;
            });
          };
          //进入模块列表
          $scope.goModuleList = function (p) {
            if (p.isCurrent) {
              var path = '/moduleList/' + p.id + '?ischeckedUser=' + $scope.ischeckedUser + '&name=' + p.name;
              $location.path(path);
            }
          }
        },
        templateUrl: 'template_views/procedure-list.html'
      }
    });
  };
});
